package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.neo4j.cypher.internal.compiler.v2_2.perty.recipe.Pretty.page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Division;
import com.sl.au.entity.EventRegister;
import com.sl.au.entity.GridAreaDetailInfo;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Point;
import com.sl.au.repository.GridAreaDetailInfoRepository;
import com.sl.au.repository.GridAreaInfoRepository;
import com.sl.au.service.GridAreaInfoService;

@Service
@Transactional
public class GridAreaInfoServiceImpl extends BaseServiceImpl implements GridAreaInfoService {

	@Autowired
	private GridAreaInfoRepository gridAreaInfoRepository;
	@Autowired
	private GridAreaDetailInfoRepository gridAreaDetailInfoRepository;
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public GridAreaInfoRepository getRepository() {

		return gridAreaInfoRepository;
	}

	@Override
	public GridAreaDetailInfoRepository getDetailRepository() {

		return gridAreaDetailInfoRepository;
	}

	@Override
	public List<GridAreaInfo> getPage(int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		return gridAreaInfoRepository.findAll(pageable).getContent();
	}

	@Override
	public List<GridAreaInfo> getBindDivisionPage(int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		return gridAreaInfoRepository.getBindDivisionPage(pageable).getContent();
	}

	@Override
	public List<GridAreaInfo> getUnbindDivisionPage(String gridTypeId,int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		return gridAreaInfoRepository.getUnbindDivisionPage(gridTypeId,pageable).getContent();
	}

	@Override
	public void deleteById(String id) {
		gridAreaInfoRepository.delete(gridAreaInfoRepository.findOne(id));
	}

	@Override
	public List<GridAreaInfo> getPageByKey(int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");

		return gridAreaInfoRepository.getPageByKey(key, pageable).getContent();
	}

	@Override
	public List<GridAreaInfo> getBindDivisionPageByKey(int start, int length, String key, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		return gridAreaInfoRepository.getBindDivisionPageByKey(key, pageable).getContent();
	}

	@Override
	public List<GridAreaInfo> getUnbindDivisionPageByKey(String gridTypeId,int start, int length, String key, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		return gridAreaInfoRepository.getUnbindDivisionPageByKey(key,gridTypeId,pageable).getContent();
	}

	@Override
	public Integer getCount(String key) {
		if (!StringUtils.hasText(key)) {
			return gridAreaInfoRepository.findAll().size();
		}
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getPageByKey(key, pageable).getContent().size();
	}

	@Override
	public Integer getBindCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return gridAreaInfoRepository.getBindDivisionPage(pageable).getContent().size();
		}

		return gridAreaInfoRepository.getBindDivisionPageByKey(key, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> getPageByArea(String gridTypeId,int start, int length, String areaId, String key, String orderName,
			Direction dir) {
		Page<GridAreaInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.gridAreaInfoRepository.getBindDivisionPageWithAreaIdByKey(areaId, key,gridTypeId, pageable);
		} else {
			pages = this.gridAreaInfoRepository.getBindDivisionPageWithAreaId(areaId,gridTypeId, pageable);
		}

		return pages.getContent();
	}
	@Override
	public List<GridAreaInfo> getPageByType(String gridTypeId,int start, int length, String key,
			String orderName, Direction dir){
		Page<GridAreaInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.gridAreaInfoRepository.getPageWithTypeNameByKey(gridTypeId, key, pageable);
		} else {
			pages = this.gridAreaInfoRepository.getPageWithTypeName(gridTypeId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<GridAreaInfo> getPointPageByArea(int start, int length, String areaId, String key, String orderName,
			Direction dir) {
		Page<GridAreaInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.gridAreaInfoRepository.getPointBindDivisionPageWithAreaIdByKey(areaId, key, pageable);
		} else {
			pages = this.gridAreaInfoRepository.getPointBindDivisionPageWithAreaId(areaId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getPointPageByAreaCount(String areaId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		int count = 0;
		if (StringUtils.hasText(key)) {
			count = this.gridAreaInfoRepository.getPointBindDivisionPageWithAreaIdByKey(areaId, key, pageable)
					.getContent().size();
		} else {
			count = this.gridAreaInfoRepository.getPointBindDivisionPageWithAreaId(areaId, pageable).getContent()
					.size();
		}
		return count;
	}

	@Override
	public List<GridAreaInfo> getPointUnbindDivisionPageByKey(int start, int length, String key, String orderName,
			Direction dir) {
		Page<GridAreaInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.gridAreaInfoRepository.getPointUnbindDivisionPageByKey(key, pageable);
		} else {
			pages = this.gridAreaInfoRepository.getPointUnbindDivisionPage(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getPointUnbindCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		int count = 0;
		if (StringUtils.hasText(key)) {
			count = this.gridAreaInfoRepository.getPointUnbindDivisionPageByKey(key, pageable).getContent().size();
		} else {
			count = this.gridAreaInfoRepository.getPointUnbindDivisionPage(pageable).getContent().size();
		}
		return count;
	}

	@Override
	public Integer getBindCountWithArea(String gridTypeId,String areaId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return gridAreaInfoRepository.getBindDivisionPageWithAreaId(areaId,gridTypeId, pageable).getContent().size();
		}

		return gridAreaInfoRepository.getBindDivisionPageWithAreaIdByKey(areaId, key,gridTypeId ,pageable).getContent().size();
	}

	@Override
	public Integer getUnbindCount(String key,String gridTypeId) {

		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return gridAreaInfoRepository.getUnbindDivisionPage(gridTypeId,pageable).getContent().size();
		}

		return gridAreaInfoRepository.getUnbindDivisionPageByKey(key,gridTypeId, pageable).getContent().size();
	}

	@Override
	public void saveSelfAndDetails(GridAreaInfo gridAreaInfo, List<GridAreaDetailInfo> detailInfos) {
		if (detailInfos != null && detailInfos.size() != 0) {
			List<GridAreaDetailInfo> dlist = gridAreaDetailInfoRepository.findByGridId(gridAreaInfo.getId());
			gridAreaDetailInfoRepository.delete(dlist);
		}

		gridAreaInfoRepository.save(gridAreaInfo);
		gridAreaDetailInfoRepository.save(detailInfos);

		/*
		 * for (GridAreaDetailInfo gridAreaDetailInfo : detailInfos) {
		 * gridAreaDetailInfo.setGridInfo(gridAreaInfo);
		 * gridAreaDetailInfoRepository.save(gridAreaDetailInfo); }
		 */

	}

	@Override
	public void deleteSelfAndDetails(GridAreaInfo gridAreaInfo) {

		List<GridAreaDetailInfo> detailInfos = gridAreaDetailInfoRepository.findByGridId(gridAreaInfo.getId());
		gridAreaDetailInfoRepository.delete(detailInfos);
		gridAreaInfoRepository.delete(gridAreaInfo);
	}

	/*
	 * 计算面是否重叠的算法
	 */
	@Override
	public boolean JudgePolygonOverlap(List<Point> mPoints,String gridId,Map<String, Object> map) {
		if (mPoints.size() <= 2)
			return false;

		List<Map<String, Object>> arrys = this.jdbcTemplate
				.queryForList("select gridAreaId,longtitude,latitude from sl_wcsi_gridareadetail "
						+ " where gridAreaId!='"+gridId+"' AND gridAreaId in(select id from sl_wcsi_gridarea where sl_wcsi_gridarea.optionTypeId in(select id from sl_dcms_option where sl_dcms_option.sGroup='元素类型' and sl_dcms_option.sTitle='网格')) "
						+ "order by gridAreaId,longlatOrder asc");
		String gridAreaId = "";
		List<Point> top = new LinkedList<>();
		for (Map<String, Object> arry : arrys) {
			if (!gridAreaId.equals(arry.get("gridAreaId"))) {
				if (isPolygonOverlap(mPoints, top)) {
					map.put("grid", this.gridAreaInfoRepository.getOne(gridAreaId));
					return true;
				}
				top = new LinkedList<>();
				gridAreaId = (String) arry.get("gridAreaId");
			}
			Point temp = new Point();
			temp.x = (double) arry.get("longtitude");
			temp.y = (double) arry.get("latitude");
			top.add(temp);
		}
		if (isPolygonOverlap(mPoints, top)) {
			map.put("grid", this.gridAreaInfoRepository.getOne(gridAreaId));
			return true;
		}
		return false;
	}

	private boolean isPolygonOverlap(List<Point> mPoints, List<Point> top) {
		Point point = new Point();
		if (top.size() > 2 && mPoints.size() > 2) {
			// 判断点是否再面内;在则返回相交，不在则继续判断
			for (int i = 0; i < mPoints.size(); i++) {
				point.x = mPoints.get(i).x;
				point.y = mPoints.get(i).y;
				if (this.isPolygonContainsPoint(top, point)) {
					return true;
				}
			}
			//再次判断点是否在面内；在则返回相交，不在则继续判断
			for (int i = 0; i < top.size(); i++) {
				point.x = top.get(i).x;
				point.y = top.get(i).y;
				if (this.isPolygonContainsPoint(mPoints, point)) {
					return true;
				}
			}
			// 判断线段是否相交
			for (int i = 0; i < mPoints.size() - 1; i++) {
				for (int j = 0; j < top.size() - 1; j++) {
					if (isLineIntersect(mPoints.get(i), mPoints.get(i + 1), top.get(j), top.get(j + 1))) {
						return true;
					}
				}
			}
		}
		return false;
	}

	private boolean isLineIntersect(Point a, Point b, Point c, Point d) {

		// 三角形abc 面积的2倍
		double area_abc = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);

		// 三角形abd 面积的2倍
		double area_abd = (a.x - d.x) * (b.y - d.y) - (a.y - d.y) * (b.x - d.x);

		// 面积符号相同则两点在线段同侧,不相交 (对点在线段上的情况,本例当作不相交处理);
		if (area_abc * area_abd >= 0) {
			return false;
		}

		// 三角形cda 面积的2倍
		double area_cda = (c.x - a.x) * (d.y - a.y) - (c.y - a.y) * (d.x - a.x);
		// 三角形cdb 面积的2倍
		// 注意: 这里有一个小优化.不需要再用公式计算面积,而是通过已知的三个面积加减得出.
		double area_cdb = area_cda + area_abc - area_abd;
		if (area_cda * area_cdb >= 0) {
			return false;
		}

		return true;
		// 计算交点坐标
		/*
		 * var t = area_cda / ( area_abd- area_abc ); var dx= t*(b.x - a.x), dy=
		 * t*(b.y - a.y); return { x: a.x + dx , y: a.y + dy };
		 */

	}

	/*
	 * 判断点是否落在面里面 (non-Javadoc)
	 * 
	 * @see
	 * com.sl.au.service.GridAreaInfoService#GetGridAreaInfoByCoordinate(double,
	 * double)
	 */
	@Override
	public GridAreaInfo GetGridAreaInfoByCoordinate(double longitude, double latitude) {
		List<Map<String, Object>> arrys = this.jdbcTemplate
				.queryForList("select gridAreaId,longtitude,latitude from sl_wcsi_gridareadetail "
						+ " where gridAreaId in(select id from sl_wcsi_gridarea where sl_wcsi_gridarea.optionTypeId in(select id from sl_dcms_option where sl_dcms_option.sGroup='元素类型' and sl_dcms_option.sTitle='网格')) "
						+ "order by gridAreaId,longlatOrder asc");
		String gridAreaId = "";

		List<Point> top = new LinkedList<>();
		Point point = new Point();

		point.x = longitude;
		point.y = latitude;

		for (Map<String, Object> arry : arrys) {
			if (!gridAreaId.equals(arry.get("gridAreaId"))) {
				if (this.isPolygonContainsPoint(top, point)) {
					return this.gridAreaInfoRepository.findOne(gridAreaId);
				}
				top = new LinkedList<>();
				gridAreaId = (String) arry.get("gridAreaId");
			}
			Point temp = new Point();
			temp.x = (double) arry.get("longtitude");
			temp.y = (double) arry.get("latitude");
			top.add(temp);
		}

		if (this.isPolygonContainsPoint(top, point)) {
			return this.gridAreaInfoRepository.findOne(gridAreaId);
		}

		return null;
	}

	public boolean isPolygonContainsPoint(List<Point> mPoints, Point point) {
		int nCross = 0;
		for (int i = 0; i < mPoints.size(); i++) {
			Point p1 = mPoints.get(i);
			Point p2 = mPoints.get((i + 1) % mPoints.size());
			// 取多边形任意一个边,做点point的水平延长线,求解与当前边的交点个数
			// p1p2是水平线段,要么没有交点,要么有无限个交点
			if (p1.y == p2.y)
				continue;
			// point 在p1p2 底部 --> 无交点
			if (point.y < Math.min(p1.y, p2.y))
				continue;
			// point 在p1p2 顶部 --> 无交点
			if (point.y >= Math.max(p1.y, p2.y))
				continue;
			// 求解 point点水平线与当前p1p2边的交点的 X 坐标
			double x = (point.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
			if (x > point.x) // 当x=point.x时,说明point在p1p2线段上
				nCross++; // 只统计单边交点
		}
		// 单边交点为偶数，点在多边形之外 ---
		return (nCross % 2 == 1);
	}

	public boolean isPointInPolygonBoundary(List<Point> mPoints, Point point) {
		for (int i = 0; i < mPoints.size(); i++) {
			Point p1 = mPoints.get(i);
			Point p2 = mPoints.get((i + 1) % mPoints.size());
			// 取多边形任意一个边,做点point的水平延长线,求解与当前边的交点个数

			// point 在p1p2 底部 --> 无交点
			if (point.y < Math.min(p1.y, p2.y))
				continue;
			// point 在p1p2 顶部 --> 无交点
			if (point.y > Math.max(p1.y, p2.y))
				continue;

			// p1p2是水平线段,要么没有交点,要么有无限个交点
			if (p1.y == p2.y) {
				double minX = Math.min(p1.x, p2.x);
				double maxX = Math.max(p1.x, p2.x);
				// point在水平线段p1p2上,直接return true
				if ((point.y == p1.y) && (point.x >= minX && point.x <= maxX)) {
					return true;
				}
			} else { // 求解交点
				double x = (point.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
				if (x == point.x) // 当x=point.x时,说明point在p1p2线段上
					return true;
			}
		}
		return false;
	}

	@Override
	public List<GridAreaInfo> getPageByDivision(int start, int length, Division division, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");

		return gridAreaInfoRepository.findByDivision(division, pageable).getContent();
	}

	@Override
	public int getCountByDivision(Division division) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findByDivision(division, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> getPageByDivisionAndKey(int start, int length, String divisionId, String type, String key,
			String orderName, String dir) {
		Page<GridAreaInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (StringUtils.hasText(key)) {
			pages = gridAreaInfoRepository.findByDivisionAndTypeAndKey(divisionId, type, key, pageable);
		} else {
			pages = gridAreaInfoRepository.findByDivisionAndType(divisionId, type, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getCountByDivisionAndKey(String divisionId, String type, String key) {
		int count = 0;
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			count = gridAreaInfoRepository.getCountByDivisionAndTypeAndKey(divisionId, type, key);
		} else {
			count = gridAreaInfoRepository.getCountByDivisionAndType(divisionId, type);
		}
		return count;
	}

	@Override
	public List<GridAreaInfo> getGridBindUserPageByKey(int start, int length, String key, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getGridBindUserPageByKey(key, pageable).getContent();
	}

	@Override
	public Integer getGridBindUserCount(String key) {

		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getGridBindUserPageByKey(key, pageable).getContent().size();
	}

	/*@Override
	public List<GridAreaInfo> getBindDivisionPageByKeyAndCon(String gridTypeId,int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getBindDivisionPageWithAreaIdByKey(key, con,gridTypeId, pageable).getContent();
	}*/

	@Override
	public List<GridAreaInfo> getGridBindUserPageByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getGridBindUserPageByKeyAndCon(key, con, pageable).getContent();
	}

	@Override
	public Integer getGridBindUserCountByCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.getGridBindUserPageByKeyAndCon(key, con, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findBindDivisionPageByKeyAndCon(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionCountByKeyAndCon(key, con, pageable).getContent();
	}

	@Override
	public Integer findBindDivisionCountByKeyAndCon(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionCountByKeyAndCon(key, con, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findBindDivisionPageOnlyKey(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionPageOnlyKey(key, pageable).getContent();
	}

	@Override
	public Integer findBindDivisionCountOnlyKey(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionPageOnlyKey(key, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findBindDivisionPageOnlyCon(String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionCountOnlyCon(con, pageable).getContent();
	}

	@Override
	public Integer findBindDivisionCountOnlyCon(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionCountOnlyCon(con, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findBindDivisionPageNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionPageNone(pageable).getContent();
	}

	@Override
	public Integer findBindDivisionCountNone(int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findBindDivisionPageNone(pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findUnbindDOnlyCon(String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDOnlyCon(con, pageable).getContent();
	}

	@Override
	public Integer findUnbindDCountOnlyCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDOnlyCon(con, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findUnbindDNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDNone(pageable).getContent();
	}

	@Override
	public Integer findUnbindDCountNone() {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDNone(pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findUnbindDByKeyAndCon(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDByKeyAndCon(key, con, pageable).getContent();
	}

	@Override
	public Integer findUnbindDCountByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindDByKeyAndCon(key, con, pageable).getContent().size();
	}

	@Override
	public List<GridAreaInfo> findUnbindOnlyKey(String key, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindOnlyKey(key, pageable).getContent();
	}

	@Override
	public Integer findUnbindCountOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return gridAreaInfoRepository.findUnbindOnlyKey(key, pageable).getContent().size();
	}

	@Override
	public GridAreaInfo findByName(String name) {
		
		return gridAreaInfoRepository.findByGridName(name);
	}
}
